package manage.action;

import m.appserver.AuthInfoUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.dao.GroupMenuLinkDao;
import manage.model.*;
import manage.run.ModuleInitRun;
import manage.service.GroupMenuLinkService;
import manage.service.ModuleService;

import java.sql.SQLException;
import java.util.List;

@ActionMeta(name="manage/groupMenuLink")
public class GroupMenuLinkAction extends ManageAction {
	private GroupMenuLink model;
	private AdminGroup adminGroup;
	private MenuInfo menu;
	private String adminGroupOid;
	private List<GroupMenuLink> modelList;
	public JSONMessage getModuleList() throws Exception {
		JSONMessage message=new JSONMessage(0);
		AdminLogin admin=verifyAdminLogin();
		OrgGroupView org=getSessionOrgGroup();
		message.push("defaultMenuOid", getService(ModuleService.class).fillModulesJSON("modules", message, admin.getOid(),null!=org?org.getOid():"",false));
		message.push("remindUrl", ModuleInitRun.getAdminRemind());
		return message;
	}
	@RequestMeta(params= {"adminOid","groupOid"})
	public JSONMessage getModuleList4Admin(String adminOid,String groupOid) throws Exception {
		JSONMessage message=new JSONMessage(0);
		verifyAdminOperPower("manage_system_power");
		getService(ModuleService.class).fillModulesJSON("modules", message, adminOid,groupOid,true);
		return message;
	}
	public JSONMessage getAllMenuList() throws SQLException, MException {
		JSONMessage message=new JSONMessage(0);
		ModuleService moduleService = getService(ModuleService.class);
		List<ModuleInfo> moduleList=moduleService.getModuleList();
		JSONMessage modules=new JSONMessage();

		for(ModuleInfo moduleInfo : moduleList){
			JSONMessage module=new JSONMessage();
			module.push("oid", moduleInfo.getOid());
			module.push("name", moduleInfo.getName());
			module.push("type", moduleInfo.getType());
			module.push("icon", moduleInfo.getIcoStyle());
			module.push("disabled","Y".equals(moduleInfo.getIsPublic()));
			JSONMessage menus1=new JSONMessage();
			List<MenuInfo> menulist1=moduleService.getMenuList(moduleInfo.getOid());
			if(!menulist1.isEmpty()){
				for(MenuInfo menuInfo1 : menulist1){
					JSONMessage menu1=new JSONMessage();
					menu1.push("oid", menuInfo1.getOid());
					menu1.push("name", menuInfo1.getName());
					menu1.push("icon", menuInfo1.getIcoStyle());
					menu1.push("description", menuInfo1.getDescription());
					menu1.push("disabled","Y".equals(menuInfo1.getIsPublic()));
					List<MenuInfo> menulist2=moduleService.getMenuList(moduleInfo.getOid(), menuInfo1.getOid());
					if(!menulist2.isEmpty()){
						JSONMessage menu2=new JSONMessage();
						for(MenuInfo menuInfo2 : menulist2){
							JSONMessage menu=new JSONMessage();
							menu.push("oid", menuInfo2.getOid());
							menu.push("name", menuInfo2.getName());
							menu.push("icon", menuInfo2.getIcoStyle());
							menu.push("desc", menuInfo2.getDescription());
							menu.push("disabled","Y".equals(menuInfo2.getIsPublic()));
							menu2.push(menuInfo2.getOid(), menu);
						}
						menu1.push("menus", menu2);
					}
					menus1.push(menuInfo1.getOid(), menu1);
				}
				module.push("menus", menus1);
			}
			modules.push(moduleInfo.getOid(), module);
		}
		message.push("modules", modules);
		return message;
	}
	public JSONMessage getGroupMenuLink() throws Exception {
		verifyAdminOperPower("manage_system_power");
		return new JSONMessage(0)
				.push("map", getDao(GroupMenuLinkDao.class).getGroupMenuLink(adminGroupOid));
	}
	public JSONMessage saveAll() throws Exception {
		verifyAdminOperPower("manage_system_power");
		getService(GroupMenuLinkService.class).saveAll(adminGroupOid,modelList);
		return new JSONMessage(0,"保存成功");
	}
	public JSONMessage gotoMenuPage() throws Exception{
		AdminLogin admin=verifyAdminLogin();
		OrgGroupView org=getSessionOrgGroup();
		IMenuName im=getService(ModuleService.class).getMenuUrl(menu.getOid(),admin.getOid(),null!=org?org.getOid():"");
		if(null==im) throw new MException(this.getClass(),"没有该菜单或权限"+menu.getOid());
		systemLog("进入菜单",admin.getRealname()+"打开【"+im.getName()+"】菜单。");
		if(StringUtil.isSpace(im.getUrlPath())){
			throw AuthInfoUtil.noPowerException;
		}
		return new JSONMessage(0)
				.push("url", im.getUrlPath());
	}
	public JSONMessage addGroupMenuLink() throws Exception {
		verifyAdminOperPower("manage_system_power");
		getService(GroupMenuLinkService.class).addGroupMenuLink(model);
		return new JSONMessage(0,"添加权限成功");
	}
	public JSONMessage removeGroupMenuLink() throws Exception {
		verifyAdminOperPower("manage_system_power");
		getService(GroupMenuLinkService.class).removeGroupMenuLink(model);
		return new JSONMessage(0,"移除权限成功");
	}
	public AdminGroup getAdminGroup() {
		return adminGroup;
	}
	public void setAdminGroup(AdminGroup adminGroup) {
		this.adminGroup = adminGroup;
	}
	public GroupMenuLink getModel() {
		return model;
	}
	public void setModel(GroupMenuLink model) {
		this.model = model;
	}
	public MenuInfo getMenu() {
		return menu;
	}
	public void setMenu(MenuInfo menu) {
		this.menu = menu;
	}
	public String getAdminGroupOid() {
		return adminGroupOid;
	}
	public void setAdminGroupOid(String adminGroupOid) {
		this.adminGroupOid = adminGroupOid;
	}
	public List<GroupMenuLink> getModelList() {
		return modelList;
	}
	public void setModelList(List<GroupMenuLink> modelList) {
		this.modelList = modelList;
	}
}
